Skip to content

Require a redirect when a pull request removes a docs page - #589

Draft
C-Mejlak wants to merge 20 commits into
mainfrom
christian/eff-1271-docs-require-a-redirect-when-a-pr-removes
Draft

C-Mejlak wants to merge 20 commits into
mainfrom
christian/eff-1271-docs-require-a-redirect-when-a-pr-removes

Conversation

@C-Mejlak

@C-Mejlak C-Mejlak commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

This adds a pull request check that fails a change that takes a docs URL away without a redirect, or that adds a malformed redirect. Draft: don't merge before smallstep/smallstep.com#1303 is in production. Until then, a redirect entry does nothing.

Why

  • Docs pages go live when this repo merges. Redirects for removed pages lived in smallstep.com's redirects.js and went live only with a site deploy, so authors forgot them.
  • 29 docs URLs have been removed from manifest.json since 2023. /docs/tutorials/mutual-tls-aws (Zero vale errors #493) returned 404 for about 10 months, and four Wi-Fi and MDM guides (Add Protect section: Wireless, Wired, and SSH for GitHub use-case guides #529) for about 4.5 months.
  • smallstep/smallstep.com#1303 serves a top-level redirects array in manifest.json as 308s. This check makes sure changes add to it.

What changed

  • "redirects": [] in manifest.json.
  • .github/scripts/check-redirects.mjs compares the pull request's merge commit with its base. It uses only git and Node built-ins.
Rule What it checks Introduced by the change Already on main
R1 A URL that works before (a page or a redirect source) still works after. Pages under /docs/step-cli/reference/ are exempt, because the site sends a dead reference URL to its nearest parent page. error n/a
R2–R6 Each redirect entry has exactly a string source and destination, written as the site matches them, and the destination is a page. No URL is both a page and a source, and no source appears twice. error warning
R7–R8 Every manifest path has a file, and no two paths share a URL. error warning
R9 A link to a page the change removes, even when the change adds a redirect for it. error; warning in generated reference pages n/a
R9b A link to any other docs URL that isn't a page. warning warning
  • Each finding is annotated at its file and line, and listed in the run summary. Text from the change can't act as a workflow command or as Markdown.
  • check-redirects.yml runs on every pull request:
    • no paths filter, so it always reports as a required check;
    • on edited as well, so a change of base branch runs it again;
    • with a read-only token, and actions pinned to commit SHAs;
    • and it fails unless the checkout is the merge commit.
  • check-redirects-tests.yml runs the script's tests when the script or these workflows change.
  • CONTRIBUTING.md gets a section, "Removing, renaming, or moving a page". AGENTS.md gets the rule and the local command.

What changes for authors

  • A change that removes or renames a page must add a redirect and update the links to the old URL in the same pull request. For example, removing step-ca/templates would mean updating 25 links in 9 files.
  • A problem already on main never fails an unrelated change. It shows as a warning.
  • To check before pushing: node .github/scripts/check-redirects.mjs. Add --base upstream/main if origin is a fork.

Verification

  • node --test '.github/scripts/**/*.test.mjs' passes 104 tests. Each rule's test was committed before its implementation.
  • Replays of real history:
  • A full run takes about 0.3 s. A hostile 100,000-character line takes about 120 ms.
  • A simulated CI run, on a depth-2 clone of a merge commit, passes the merge-commit step.
  • actionlint is clean. Vale wasn't run locally; Lint Prose decides on this pull request.

Existing warnings

  • certificate-manager/kubernetes-tls/kubernetes-step-issuer.mdx:32 links to /docs/step-cli/reference/beta/ca/provisioner/add, which step-ci removed in 2023.
  • step-ca/certificate-authority-server-production.mdx:837 links to /docs/cli/crypto/jwt/verify.
  • tutorials/protect-wired-networks.mdx:30 has a relative JSX link. The site renders JSX as written, so the browser resolves it to /docs/tutorials/protect-wired-networks/protect-wireless-networks.mdx, which works only through an entry in smallstep.com's redirects.js.

Order

  1. smallstep/smallstep.com#1303 reaches production.
  2. Merge this.
  3. A docs admin adds check-redirects to the required checks on main.
  4. The first real page removal fails until it adds a redirect. After merge, the old URL returns 308.

A PR that removes a docs page must add a redirect, or readers get a 404. Pages under the CLI reference are exempt, because the site redirects a dead reference URL to its nearest parent page, but the reference root is not. An unreadable manifest is reported on its own: invalid JSON, or JSON in a shape the site can't read.
checkRedirects compares a PR's head with its base. A page URL that works on the base and not on the head is an error with a redirect entry to paste, unless it is under the CLI reference. readManifest rejects JSON smallstep.com can't walk, and the error names the bad route.
The check compares a head, the working tree by default, with the merge base of --base and that head, so a branch that is behind main isn't blamed for main's changes. In GitHub Actions it prints annotations and writes the run summary: errors first, then new warnings, then warnings already on the base. Elsewhere it prints a plain list. It exits 1 on any error and 2 when it can't read a revision.
check-redirects.mjs reads each revision with one git cat-file process: the commit's file list, manifest.json and every .mdx file. The working tree is read in place, untracked files included. report.mjs writes annotations, the run summary and a plain list. A full run over the docs repo takes about a quarter of a second.
Both removed pages without a redirect. #529 removed four Wi-Fi and MDM guides that returned 404 for about 4.5 months, and #493 removed a tutorial that returned 404 for about 10. Each replay must report exactly those URLs. In a shallow clone the replays skip.
Every page in the docs manifest sits under a heading route, so R1 checks nothing if the walk stops recursing. Until now only the replays caught that, and they skip in a shallow clone. The spec also says warnings never fail the check, which no test covered. Each new test fails when its behavior is broken.
Git allows a newline in a file name. readCommit sends one <commit>:<path> line per file to git cat-file --batch, so such a name becomes two requests, and every later file gets the text of the one before it. A branch that removes a page can then pass: a decoy file sits where manifest.json is read. These tests fail until contents are read by object id.
readCommit lists the tree with object ids and asks git cat-file --batch for blobs by id, so a file name can no longer reach the batch protocol. Each answer must carry the requested id, be a blob and have a numeric size, and no output may be left over; anything else fails the check with exit 2. Error messages carry object ids, not file names.
Messages quote text from the change: URLs built from manifest paths now, and link targets and file names later. V8's JSON errors also quote the manifest around the bad token, newlines included. On a GitHub runner, a printed line that starts with :: or holds ##[ is a workflow command, so a manifest path can forge annotations in the plain list or in git's error text. The summary is Markdown, where the same text can add links, images or headings.

The tests expect:
- one line per violation in the plain list;
- in Actions, the plain list and stderr wrapped in a stop-commands block;
- the change's text in the summary rendered as plain text;
- JSON errors that give a position instead of quoting the manifest.
- In GitHub Actions, everything printed besides the annotations sits in a stop-commands block with a random token: the plain list, and the usage and git errors on stderr. The runner then reads none of it as a workflow command.
- The plain list and the summary make line breaks visible as ⏎ and replace other control characters, so each violation stays on one line.
- The summary escapes all ASCII punctuation in locations and messages, so text from the change renders as written.
- A JSON syntax error now gives only its line and column instead of V8's message, which quotes the manifest around the error.
`redirects` in manifest.json must be an array of objects with exactly a string source and a string destination (R2). A source is /docs plus at least one segment, written as smallstep.com matches it (R3). A destination is a docs page in the change, optionally with a fragment (R4). No URL may be both a page and a source (R5), and no source may appear twice (R6).

An entry that passes R2–R4 keeps its source working for R1, so removing it, or pointing it at a missing page, fails like removing a page. A problem already on the base is a warning, and so is every finding when the base manifest can't be read.
readSite collects what one revision serves: its pages, plus the sources of redirects that pass R2–R4. That is what R1 compares. It also collects the revision's own problems (R2–R6), each keyed by rule and entry. A problem in the head becomes an error when the base doesn't have its key, and a warning otherwise.

Annotations point at the line of the entry's source. An entry with a malformed source gets only R3 and R4, since R5 and R6 can't apply to a URL smallstep.com would never match.
A manifest path whose file doesn't exist is a page smallstep.com can't render (R7). Two manifest paths that map to the same URL, such as /foo.mdx and /foo/README.mdx, make one of them unreachable (R8). Both are errors when the change introduces them and warnings when the base already has them.

The test revision now gives every manifest path a file unless a test lists the files itself.
pageFindings reports a manifest path with no file (R7) and a URL that more than one path maps to (R8), each at the line of its "path". The R7 and R8 tests expected the title's line, because the test routes use the path as their title. They now look for the "path" line.
docsLinks reads the docs URLs an .mdx file links to, and the line of each:
- inline links, reference definitions, JSX href, and autolinks or bare https://smallstep.com/docs URLs;
- skipped: images, footnotes, fenced and inline code, other schemes and hosts, anchors and assets.

Each link resolves the way smallstep.com renders it:
- Markdown links as remark-relative-links.js rewrites them, against the file's directory;
- JSX hrefs as the browser resolves them, since the site leaves them alone;
- then the fragment, query string, trailing slash, and trailing /index.html or /README are dropped.

It must stay fast on hostile input. A link to a page the change removes fails (R9), even when the change adds a redirect. It only warns when the file is generated under step-cli/reference/. A link to any other non-page is a warning (R9b).
links.mjs reads the links of each .mdx file one line at a time, outside fenced and inline code. Every pattern has bounded repeats: a 100k-character line of "[" takes about 120 ms.

How a link resolves:
- A Markdown link or definition resolves as remark-relative-links.js rewrites it.
- A JSX href resolves from the page URL as a browser would. The site renders JSX untouched, so <a href="./x.mdx"> on /docs/a/b/ leads to /docs/a/b/x.mdx.

pageUrl moves to links.mjs, so rules.mjs depends on links.mjs and not the other way round.

R9 is an error for a link to a page the change removes, and a warning when a generated reference page has it. R9b warns about any other link to a non-page.

On main (247185f) the check reports three existing R9b warnings, not the two the spec expects. The third is tutorials/protect-wired-networks.mdx:30, whose relative JSX href leads to /docs/tutorials/protect-wired-networks/protect-wireless-networks.mdx. smallstep.com answers that URL only through a redirects.js entry.
The step-ci push 5184bd8 removed ten beta/ca reference pages without a redirect, which the reference exemption allows. It fails only for the hand-written link to one of them, at certificate-manager/kubernetes-tls/kubernetes-step-issuer.mdx:31. Main at 247185f has no errors and exactly three existing R9b warnings. A full run over main takes about 0.27 s.
check-redirects.yml is meant to become a required check:
- It runs on every pull request, with no paths filter, and on `edited` too, so changing the base branch runs it again.
- It checks out the merge commit with fetch-depth 2 and fails if HEAD has no second parent, because only then is HEAD^1 the base branch.
- It has a read-only token, no persisted credentials, no secrets and no job-level `if`. A skipped required check counts as passing.

check-redirects-tests.yml runs the script's tests when the script or these workflows change. It fetches full history for the replays. Both pin actions/checkout v7.0.1 and actions/setup-node v7.2.0 to commit SHAs and use Node 24. actionlint reports nothing.
smallstep.com serves each entry of a top-level "redirects" array in manifest.json as a 308 (smallstep/smallstep.com#1303). The list starts empty, and each later change that removes a docs URL adds its redirect here.

The reference update in smallstep/cli's release workflow replaces only the routes of the node marked isStepReference, so it keeps this key.
CONTRIBUTING.md gets a section on the redirect a change needs when it takes a docs URL away:
- the entry format;
- that the destination must be a page;
- updating links to the old URL in the same pull request;
- why reference pages need none;
- how to run the check locally, including when origin is a fork.

AGENTS.md gets the same rule and the local command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant